<?php

session_start();
$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : 'main';
$resp = array('code'=>-1, 'message'=>'Error');
$isLogin = is_login();
if($act == 'main') {
	echo '<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>登录成功</title></head><body>';
	if($isLogin) {
		echo '<a href="user.php?act=logout">退出</a>';
		echo '<pre>';
		var_dump($_SESSION);
		echo '</pre>';
	}
	else {
		echo '<a href="user.php?act=login">登录</a>';
	}
	echo '</body></html>';
	exit;
}

elseif($act == 'get_user_info') {
	if($isLogin) {
		$resp['code'] = 0;
		$resp['message'] = 'Success';
		$result = array();
		$uid = $_SESSION['user_id'];
		$result['user_id'] = $uid;
		$result['user_name'] = $_SESSION['user_name'];
		$result['avatar'] = $_SESSION['avatar'];
		$resp['my'] = $result;
		$user = new User;
		$friends = $user->get_friends($uid);
		foreach($friends as &$friend) {
			$friend['avatar'] = get_avatar($friend['user_id']);
		}
		$resp['friends'] = $friends;
	}
	else {
		$resp['code'] = -101;
	}
	echo json_encode($resp);
	exit;
}

elseif($act == 'do_login') {
	$email = $_POST['email'];
	$pwd = $_POST['password'];
	$user = new User;
	$result = $user->get_user_info($email);
	if($result) {
		$uid = $result['user_id'];
		$result['avatar'] = get_avatar($uid);
		$_SESSION['user_id'] = $uid;
		$_SESSION['user_name'] = $result['user_name'];
		$_SESSION['avatar'] = $result['avatar'];
		$resp['code'] = 0;
		$resp['message'] = 'Success';
		$resp['data'] = $result;
	}
	echo '<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>登录成功</title></head><body>';
	echo '<h1>登录成功！<span id="sec">3</span>秒后自动跳转...</h1>';
//	echo '<pre>';
//	var_dump($resp);
//	echo '</pre>';
	echo '<script>setInterval(function(){sec.innerText = parseInt(sec.innerText) - 1}, 1000);setTimeout(function(){location.href="../"}, 3000);</script>';
	echo '</body></html>';
	exit;
}

elseif($act == 'login') {
?>
	<!DOCTYPE html>
	<html lang="zh-CN">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Login</title>
	</head>
	<body>
	<form action="user.php?act=do_login" method="post">
		<table style="width: 600px;margin: 0 auto;">
			<tr>
				<td width="80">Email</td>
				<td><input type="text" name="email"/></td>
			</tr>
			<tr>
				<td width="80">密码</td>
				<td><input type="password" name="password"/></td>
			</tr>
			<tr>
				<td width="80"></td>
				<td><button type="submit">登录</button></td>
			</tr>
		</table>
	</form>
	</body>
	</html>
<?php
}

elseif($act == 'logout') {
	session_destroy();
	header('location:user.php');
}

function is_login() {
	$bool = false;

	if(isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0) {
		$bool = true;
	}

	return $bool;
}

function get_avatar($uid) {
	return 'asset/avatar/u'.($uid % 11).'.jpg';
}

class User {
	private $mysqli;
	function __construct() {
		$this->mysqli = new mysqli("localhost", "root", "123456", "frilly");
//		$this->mysqli = new mysqli("sql.frilly.com:3306", "frilly", "Rq2tu7f7MX5cYcMG", "frilly");
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$this->mysqli->query('SET NAMES UTF8');
	}

	function get_user_info($email) {
		$sql = 'SELECT * FROM ecs_users WHERE email="'.$email.'"';
		$data = '';
		if ($qry = $this->mysqli->query($sql)) {
			$data = $qry->fetch_assoc();
			$qry->free();
		}
		return $data;
	}

	function get_friends($uid) {
		$sql = 'SELECT user_id,email,user_name,(SELECT COUNT(*) FROM ecs_chat WHERE to_uid='.$uid.' AND uid=user_id AND read_time=0) new_msg_num FROM ecs_users WHERE user_id!="'.$uid.'"';
		$data = array();
		if ($qry = $this->mysqli->query($sql)) {
			while($row = $qry->fetch_assoc()) {
				$data[] = $row;
			}
			$qry->free();
		}
		return $data;
	}

	function __destruct() {
		$this->mysqli->close();
	}
}